package main

import "fmt"

/*
@author: liujun
@file: 二叉树.go
@time: 2022/12/4 13:45
*/
//节点
type Node struct {
	data int
	left *Node
	right *Node
}
//树
type Tree struct {
	root *Node
}
//向树中添加元素
func (tree *Tree) add(i int){
	//实例化节点
	node := &Node{data: i}
	fmt.Println("增加节点", i)
	if tree.root == nil{
		tree.root = node
	}else {
		//切片储存节点地址
		queue := []*Node{tree.root}
		for len(queue) !=0 {
			//将第一个元素设为根
			cur := queue[0]
			//弹出第一个元素
			queue = queue[1:]
			if cur.left == nil {
				cur.left = node
				queue = append(queue, cur.left)  //将节点地址加入到切片
			}else if cur.right == nil {
				cur.right = node
				queue = append(queue, cur.right)
			}
		}
	}
}

func main() {
	var t Tree
	for i:=1; i <= 20; i++{
		t.add(i)
	}
}